package com.sophialex.health.record.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.showKtable.ShowKTable;
import com.sophialex.health.system.frame.resource.RcpResourceManager;
import com.sophialex.health.system.frame.util.Assert;
import com.sophialex.health.system.frame.util.LayoutUtil;





public class HrPersonSelectDlg extends Dialog {

	private Button buttonQd;
	protected Object result;
	protected Shell shell;
	private List<HrPerson> list = new ArrayList<HrPerson>();
	private ShowKTable showTable;
	private Button buttonQx;
	private Text text_search;
	protected List<HrPerson> resultList = new ArrayList<HrPerson>();
	private String name;

	/**
	 * Create the dialog
	 * 
	 * @param parent
	 * @param style
	 */
	public HrPersonSelectDlg(Shell parent, int style) {
		super(parent, style);
	}
    
	public HrPersonSelectDlg(Shell parent,String name,List<HrPerson> resultList) {
		super(parent, SWT.NONE);
		this.list = resultList;
		this.name = name;
	}
	
	/**
	 * Create the dialog
	 * 
	 * @param parent
	 */
	public HrPersonSelectDlg(Shell parent) {
		this(parent, SWT.NONE);
	}

	public HrPersonSelectDlg(Shell parent, String name) {
		this(parent, SWT.NONE);
		this.name = name;
	}

	/**
	 * Open the dialog
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		Display display = getParent().getDisplay();
		LayoutUtil.centerShell(display, shell);
		shell.open();
		if (list != null && list.size() > 0) {
			showTable.setSelection(list.get(0));
		}
		shell.layout();
		if (!Assert.isNull(this.name)) {
			this.setSerch(this.name);
		}
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(800, 600);
		shell.setLayout(new FormLayout());
		shell.setText(Messages.getString("HrPersonSelectDlg.0")); //$NON-NLS-1$
		final Composite composite = new Composite(shell, SWT.BORDER);
		composite.setLayout(new FormLayout());
		FormData formData_composite = new FormData();
		formData_composite.bottom = new FormAttachment(100, 0);
		formData_composite.top = new FormAttachment(0, 0);
		formData_composite.right = new FormAttachment(100, 0);
		formData_composite.left = new FormAttachment(0, 0);
		composite.setLayoutData(formData_composite);

		final CLabel label = new CLabel(composite, SWT.NONE);
		label.setText(Messages.getString("HrPersonSelectDlg.1")); //$NON-NLS-1$
		FormData formData_label = new FormData();
		formData_label.top = new FormAttachment(0, 535);
		formData_label.bottom = new FormAttachment(0, 555);
		formData_label.right = new FormAttachment(0, 85);
		formData_label.left = new FormAttachment(0, 35);
		label.setLayoutData(formData_label);
		text_search = new Text(composite, SWT.BORDER);
		FormData formData_text = new FormData();
		formData_text.bottom = new FormAttachment(label, 0, SWT.BOTTOM);
		formData_text.top = new FormAttachment(label, -3, SWT.TOP);
		formData_text.right = new FormAttachment(label, 100, SWT.RIGHT);
		formData_text.left = new FormAttachment(label, 0, SWT.RIGHT);
		text_search.setLayoutData(formData_text);
		buttonQd = new Button(composite, SWT.NONE);
		buttonQd.setText(Messages.getString("HrPersonSelectDlg.2")); //$NON-NLS-1$
		FormData formData_buttonQd = new FormData();
		formData_buttonQd.bottom = new FormAttachment(text_search, 0,
				SWT.BOTTOM);
		formData_buttonQd.top = new FormAttachment(text_search, 0, SWT.TOP);
		formData_buttonQd.right = new FormAttachment(text_search, 100,
				SWT.RIGHT);
		formData_buttonQd.left = new FormAttachment(text_search, 50, SWT.RIGHT);
		buttonQd.setLayoutData(formData_buttonQd);
		buttonQx = new Button(composite, SWT.NONE);
		buttonQx.setText(Messages.getString("HrPersonSelectDlg.3")); //$NON-NLS-1$
		FormData formData_buttonQx = new FormData();
		formData_buttonQx.bottom = new FormAttachment(buttonQd, 0, SWT.BOTTOM);
		formData_buttonQx.top = new FormAttachment(buttonQd, 0, SWT.TOP);
		formData_buttonQx.right = new FormAttachment(buttonQd, 50, SWT.RIGHT);
		formData_buttonQx.left = new FormAttachment(buttonQd, 0, SWT.RIGHT);
		buttonQx.setLayoutData(formData_buttonQx);
		setBackGroud(shell);
		setBackGroud(composite);
		createTable(composite);
		initData();// 初始化表格数据
		addListener();

	}

	private void doSearch() {
		String condition = this.text_search.getText();
		resultList.clear();
		int i = -1;
		int j = -1;
		int k = -1;
		int n = -1;
		List<HrPerson> list1 = new ArrayList<HrPerson>();
		List<HrPerson> list2 = new ArrayList<HrPerson>();
		for (HrPerson da : (List<HrPerson>) list) {
//			if (!Assert.isNull(da.getFmcode())) {
//				i = da.getFmcode().indexOf(condition);
//			}
//			if (!Assert.isNull(da.getFzjcpy())) {
//				j = da.getFzjcpy().toUpperCase().indexOf(
//						condition.toUpperCase());
//			}
//			if (!Assert.isNull(da.getFzjcqt())) {
//				n = da.getFzjcqt().toUpperCase().indexOf(
//						condition.toUpperCase());
//			}
			if (!Assert.isNull(da.getName())) {
				k = da.getName().indexOf(condition);
			}
			if (i == 0 || j == 0 || k == 0 || n ==0) {// 等于0，精确查找。精确查找的值放在list1里面
				list1.add(da);
			}
			if (i > 0 || j > 0 || k > 0 || n > 0) {// 大于0，模糊查找。模糊查找的值放在list2里面
				list2.add(da);
			}
		}
		resultList.addAll(list1);// 先添加精确查找的值
		resultList.addAll(list2);// 在添加模糊查找的值
		this.showTable.setInput(resultList);
		if (resultList.size() > 0)
			showTable.setSelection(resultList.get(0));

	}

	private void addListener() {
		buttonQd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				okPressed();
			}
		});
		buttonQx.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doExit();
			}
		});
		showTable.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent arg0) {

			}

			public void mouseDown(MouseEvent arg0) {

			}

			public void mouseDoubleClick(MouseEvent arg0) {
				okPressed();
			}
		});
		showTable.addKeyListener(new KeyListener() {

			public void keyReleased(KeyEvent e) {
			}

			public void keyPressed(KeyEvent e) {
				if (e.keyCode == SWT.KEYPAD_CR || e.keyCode == SWT.CR) {
					okPressed();
				}
			}
		});
		text_search.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				doSearch();
			}
		});

		text_search.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == SWT.ARROW_DOWN) {
					jumpNext();
					e.doit = false;
				} else if (e.keyCode == SWT.ARROW_UP) {
					jumpUp();
					e.doit = false;
					return;
				} else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
					okPressed();
					e.doit = false;
				}
			}
		});
	}

	protected void jumpUp() {
		int curIdx = showTable.m_FocusRow;
		if (curIdx > 1) {
			showTable.setSelection(showTable.getInput().get(curIdx - 2));
		}
	}

	protected void jumpNext() {
		int curIdx = showTable.m_FocusRow;
		if (curIdx < showTable.getInput().size()) {
			showTable.setSelection(showTable.getInput().get(curIdx));
		}
	}

	protected void okPressed() {
		HrPerson selectItem = (HrPerson) showTable.getFirstSelection();
		result = selectItem;
		shell.close();
	}

	protected void doExit() {
		result = null;
		shell.close();
	}

	private void initData() {
		if (list != null && list.size() > 0) {
			showTable.setInput(list);
			showTable.refresh();
			// showTable.setSelection(list.get(0));
		}
	}

	private void createTable(Composite composite) {
		try {
			showTable = new ShowKTable(composite, "com.sophialex.health.record.dialog" //$NON-NLS-1$
					+ "HrPersonSelectDlg", true, SWT.BORDER | SWT.H_SCROLL //$NON-NLS-1$
					| SWT.V_SCROLL | SWT.FULL_SELECTION);

			FormData formData = new FormData();
			formData.bottom = new FormAttachment(100, -45);
			formData.top = new FormAttachment(0, 0);
			formData.right = new FormAttachment(100, 0);
			formData.left = new FormAttachment(0, 0);
			showTable.setLayoutData(formData);

			showTable.setTitlesAndProperties(getColumnTitles(),
					getColumnProperties());
			showTable.setColumnAligns(getColumnAligns());
			showTable.setColumnWidths(getColumnWidths());
			showTable.setBackground(Display.getCurrent().getSystemColor(
					SWT.COLOR_LIST_BACKGROUND));
		} catch (Exception e) {
			SuperContext.showExceptionMsg(e);
		}
	}

	/**
	 * 子类实现来设置表头
	 * 
	 * @return String[]
	 */
	private String[] getColumnTitles() {
		return new String[] { Messages.getString("HrPersonAssist.0"), Messages.getString("HrPersonAssist.1"), Messages.getString("HrPersonAssist.2"), Messages.getString("HrPersonAssist.3"), Messages.getString("HrPersonAssist.4"), Messages.getString("HrPersonAssist.5")}; 
	}

	/**
	 * 子类实现来设置要显示的属性值
	 * 
	 * @return String[]
	 */
	private String[] getColumnProperties() {
		return new String[] { "name", "sex", "birthday","idno","adminUserId","cadress"}; 
	}

	/**
	 * 子类实现来设置要显示的列Alignment,如果不设置，取ShowKTable中的默认值
	 * 
	 * @return int[]
	 */
	protected int[] getColumnAligns() {
		return new int[] { ShowKTable.C, ShowKTable.C, ShowKTable.C,
				ShowKTable.C, ShowKTable.C, ShowKTable.C};
	}

	/**
	 * 子类实现来设置要显示的列宽,如果不设置，取ShowKTable中的默认值
	 * 
	 * @return int[]
	 */
	protected int[] getColumnWidths() {
		return new int[] { 80, 80, 60, 120, 80, 60};
	}

	public void setData(List<HrPerson> list) {
		this.list = list;
	}

	public void setBackGroud(Composite composite) {
		composite.setBackground(RcpResourceManager.getColor(244, 244, 247));
		for (Control con : composite.getChildren()) {
			con.setBackground(RcpResourceManager.getColor(244, 244, 247));
		}
	}

	public void setSerch(String key) {
		text_search.setText(key);
	}
}
